package com.ljxt.dt.service.storemanagementservice.impl;

import com.ljxt.dt.dao.storemanagementdao.StoreDao;
import com.ljxt.dt.dao.storemanagementdao.impl.StoreDaoImpl;
import com.ljxt.dt.domain.*;
import com.ljxt.dt.service.storemanagementservice.StoreService;
import com.ljxt.dt.util.ServiceUtil;
import com.ljxt.dt.vo.PageVO;
import com.ljxt.dt.vo.storemanagement.FindVO;
import com.ljxt.dt.vo.storemanagement.ReceptVO;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 唐春劲
 * @version 1.0.0
 * @date 2020-05-11 19:10
 * @copyright 老九学堂
 */

public class StoreServiceImpl implements StoreService {
    private StoreDao admin = new StoreDaoImpl();
    @Override
    public PageVO<ReceptVO> findAll(int pageIndex, int pageSize,FindVO findVO) {
        List<ReceptInfo> recepts = admin.findAll(pageIndex,pageSize,findVO);
        if(recepts == null){
            recepts = new ArrayList<>();
        }
        //循环创建receptVO
        List<ReceptVO> receptVOS = new ArrayList<>();

        for (ReceptInfo r:recepts) {
            String result = admin.selectResultString(r.getStateId());
            String name = admin.selectName(r.getTypeId());
            ReceptVO receptVO  = new ReceptVO();
            receptVO.setId(r.getId());
            //设置时间

            receptVO.setCreatedate(ServiceUtil.dateFormat(r.getCreatedate()));
            receptVO.setName(name);
            receptVO.setResult(result);
            receptVO.setOrderNo(r.getOrderNo());
            receptVO.setReceptNo(r.getReceptNo());
            receptVO.setRemakers(r.getRemakers());
            receptVOS.add(receptVO);
        }
        int totle = admin.selectTotle(findVO);
       //创建pageVO
        PageVO<ReceptVO> pageVO = new PageVO<ReceptVO>(receptVOS,pageIndex,pageSize,totle);

        return pageVO;
    }

    @Override
    public List<String> selectName() {
        return admin.selectName();
    }

    @Override
    public List<ReceptType> selectType() {
        return admin.selectType();
    }

    @Override
    public List<BillState> selectResult() {
        return admin.selectResult();
    }

    @Override
    public int submitOrder(String[] data) {
        //循环遍历需要提交的订单的单号
        int row = 0;
        for (String datum : data) {
            //改变审核状态
            row += admin.submitOrder(datum);
            //添加经办信息
              admin.insertBill( datum ,"张三",1,1);
        }

        return row;
    }

    @Override
    public int checkOrder(String[] data) {
        int  rows = 0;
        //循环审核订单
        for (String datum : data) {
           rows +=   admin.checkOrder(datum);
           //添加一个经办信息
            admin.insertBill(datum,"李四",2,2);
        }
        return rows;
    }

    @Override
    public int denyCheckOrder(String data) {
        int rows = 0;
        //将单据状态改为审核拒绝
        rows += admin.denyCheckOrder(data);
        //添加经办信息
        admin.insertBill(data,"李四",2,3);
        return rows;
    }

    @Override
    public int delete(String order) {
        //先删除订单
        int rows = 0;
        rows +=  admin.delete(order);
        //删除订单关联的商品
        rows += admin.deleteInfo(order);
        return rows;
    }
}
